package com.situ.ssm.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.ssm.common.ServerResponse;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IStudentService;

import net.sf.jsqlparser.statement.update.Update;

@Controller
@RequestMapping("/student")
public class StudentController {
	
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping("/getStudentListPage")
	public String getStudentListPage() {
		return "student_list";
	}
	
	// /student/pageList.action?page=1&limit=30
	@RequestMapping("/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page, Integer limit, Student student) {
		return studentService.pageList(page, limit, student);
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		return studentService.deleteById(id);
	}
	
	@RequestMapping("/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(Integer[] ids) {
		// ids: 40,41
		System.out.println(Arrays.toString(ids));// [40, 41]
		return studentService.deleteAll(ids);
	}
	@RequestMapping("/getInsertPage")
	public String getInsertPage() {
		return "student_insert";
	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public ServerResponse insert(Student student) {
		return studentService.insert(student);
	}
	
	@RequestMapping("/getUpdatePage")
	public String getUpdatePage(Integer id,Model model) {
		
		
		Student student = studentService.selectById(id);
		System.out.println(student);
		
		// 放到一块内存区域里面
		model.addAttribute("student", student);
		
		// 请求转发到student_list.jsp页面进行展示
		//req.getRequestDispatcher("/student_update.jsp").forward(req, resp);	
		return "student_update";
	}
	@RequestMapping(value = "/update")
	@ResponseBody
	public ServerResponse update(Student student)  {
		/*
		 * String id = req.getParameter("id"); String name = req.getParameter("name");
		 * String age = req.getParameter("age"); String gender =
		 * req.getParameter("gender"); Student student = new Student();
		 * student.setId(Integer.parseInt(id)); student.setName(name);
		 * student.setGender(gender); student.setAge(Integer.parseInt(age));
		 */
		boolean isSuccess = studentService.update(student);
		ServerResponse serverResponse = null;
		// 重定向到/JavaWeb1907/student
		if (isSuccess) {
			serverResponse = ServerResponse.createSuccess("修改成功");
		} else {
			serverResponse = ServerResponse.createError("修改失败");
		}
		//resp.sendRedirect(req.getContextPath() + "/student");
		//resp.sendRedirect("/JavaWeb1907/student");
		return serverResponse;
		
				
		
		
		
	}
}
/*
 * @RequestMapping(value = "/update")
 * 
 * @ResponseBody private ServerResponse update(HttpServletRequest req,
 * HttpServletResponse resp) throws IOException {
 * System.out.println("StudentServlet.update()"); String id =
 * req.getParameter("id"); String name = req.getParameter("name"); String age =
 * req.getParameter("age"); String gender = req.getParameter("gender"); Student
 * student = new Student(Integer.parseInt(id), name, Integer.parseInt(age),
 * gender); boolean isSuccess = studentService.update(student); ServerResponse
 * serverResponse = null; // 重定向到/JavaWeb1907/student if (isSuccess) {
 * serverResponse = ServerResponse.createSuccess("修改成功"); } else {
 * serverResponse = ServerResponse.createError("修改失败"); }
 * //resp.sendRedirect(req.getContextPath() + "/student");
 * //resp.sendRedirect("/JavaWeb1907/student"); return serverResponse; } }
 */
